package com.the4man.framework.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.opensymphony.xwork2.ActionSupport;
import com.the4man.cnbdw.utils.Page;
import com.the4man.framework.pagemodel.PUser;
import com.the4man.framework.util.InitParamUtil;
import com.the4man.framework.util.RequestUtil;
import com.thoughtworks.xstream.XStream;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by lenovo on 2017/12/12.
 */
@ParentPackage("defaultPackage")
public class BaseAction extends ActionSupport {
    private static final long serialVersionUID = 4723491385210687203L;
    private static final Logger logger = LoggerFactory.getLogger(BaseAction.class);

    public BaseAction() {
    }

    public void writeJson(Object obj) {
        try {
            String json = JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss", new SerializerFeature[0]);
            ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
            ServletActionContext.getResponse().getWriter().write(json);
            ServletActionContext.getResponse().getWriter().flush();
        } catch (IOException var3) {
            logger.error("对象转json出现错误:" + var3.toString());
        }

    }

    public void writeXml(XStream xstream, Object obj) {
        try {
            String xml = xstream.toXML(obj);
            ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
            ServletActionContext.getResponse().getWriter().write(xml);
            ServletActionContext.getResponse().getWriter().flush();
        } catch (IOException var4) {
            logger.error("对象转xml出现错误:" + var4.toString());
        }

    }

    public void writeXml(String xml) {
        try {
            ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
            ServletActionContext.getResponse().getWriter().write(xml);
            ServletActionContext.getResponse().getWriter().flush();
        } catch (IOException var3) {
            logger.error("对象转xml出现错误:" + var3.toString());
        }

    }

    protected PUser getSessionUser() {
        return (PUser) RequestUtil.getHttpSessionObject(InitParamUtil.getSessionInfoName());
    }

    protected String getSessionUserId() {
        return this.getSessionUser().getUserid();
    }

    public HttpServletRequest getRequest() {
        return ServletActionContext.getRequest();
    }

    public HttpServletResponse getResponse() {
        return ServletActionContext.getResponse();
    }

    public HttpSession getSession() {
        return this.getRequest().getSession();
    }

    protected int getRows() {
        String rows = this.getRequest().getParameter("rows");
        if(StringUtils.isNotBlank(rows)) {
            try {
                return Integer.valueOf(rows).intValue();
            } catch (Exception var3) {
                ;
            }
        }

        return 1;
    }

    protected int getPage() {
        String page = this.getRequest().getParameter("page");
        if(StringUtils.isNotBlank(page)) {
            try {
                return Integer.valueOf(page).intValue();
            } catch (Exception var3) {
                ;
            }
        }

        return 10;
    }

    protected int getLimitStart() {
        return Math.abs(this.getPage() - 1) * this.getRows();
    }

    protected int getRowsPerPage() {
        return Math.abs(this.getPage()) * this.getRows();
    }

    protected void setPageProperties(Page page) {
        page.setCurPage(this.getPage());
        page.setRowsPerPage(this.getRows());
    }
}
